var idsPopup=0; //Winpopup
var idsLoading=1; //Loading
var mouseX; // BodyMove
var mouseY; // BodyMove
var _dir_server=_PATH;
if (_TITLE==null) _TITLE="SISTEMA";
function disabledDiv(Div){
	Div.style.position="relative";
	OverDiv = document.createElement("div");
	OverDiv.style.height = Div.offsetHeight;
	OverDiv.style.width = Div.offsetWidth;
	OverDiv.id="ide_divOver";
	OverDiv.className="divOver";
	
	DivLoading = document.createElement("div");
	DivLoading.className="divLoading";
	Imagen = document.createElement("img");
	Imagen.id="LoadImageID";
	Imagen.className="LoadImageID";	
	Imagen.src="progress.gif";
	Espacio = document.createElement("span");
	Espacio.innerHTML="  Cargando..."
	
	DivLoading.appendChild(Imagen);
	DivLoading.appendChild(Espacio);

	OverDiv.appendChild(DivLoading);
	
	combos=Div.getElementsByTagName("SELECT");
	for(a=0;a<combos.length;a++){
		combos[a].disabled=true;
	}
	Div.appendChild(OverDiv);
}
function enabledDiv(div){
	divs=div.getElementsByTagName("div");
	divPadre = div.parentNode
	for(i=0;i<divs.length;i++){
		if (divs[i].className == "divOver") {
			div.removeChild(divs[i]);
		}
	}	
	combos=div.getElementsByTagName("SELECT");
	for(a=0;a<combos.length;a++){
		combos[a].disabled=false;
	}	
}
function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\s)'+searchClass+'(\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
function getElementsBy(type, find,node,tag) {
	var Elements = new Array();
	var array = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	if ( find == null )
		find = '*';
	var element = node.getElementsByTagName(tag);
	var len = element.length;
	var patron = new RegExp('(^|\s)'+find+'(\s|$)');
	for (i = 0; i < len; i++) {
		var str="patron.test(element[i]."+type+")";
		//alert (element[i].id);
		if (eval(str)) {
			Elements.push(element[i]);
		}
	}
	return Elements;
}
function getElementById(find,node, tag) {
	var Element;
	if ( tag == null )tag = '*';
	if ( node == null )
		node = document;
	if ( find == null )
		find = '*';
	var elements = node.getElementsByTagName(tag);
	var len = elements.length;
	var patron = new RegExp('(^|\s)'+find+'(\s|$)');
	for (i = 0; i < len; i++) {
		var str="patron.test(elements[i].id)";
		if (eval(str)) {return elements[i];	}
	}
}
function bodyWH(){
	var myWidth = 0, myHeight = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
	//Non-IE
	myWidth = window.innerWidth;
	myHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	//IE 6+ in 'standards compliant mode'
	myWidth = document.documentElement.clientWidth;
	myHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
	//IE 4 compatible
	myWidth = document.body.clientWidth;
	myHeight = document.body.clientHeight;
	}
	return [myHeight, myWidth];
}
function bodyMOVE(event){
	//document.getElementById('popupContent').innerHTML="Funciones...";
	var ID = document.getElementById('idPopup');
	if (!ID) return false;
	if (ID.value=="") return false;
	if (!document.getElementById(ID.value)) return false;
	//document.getElementById('popupContent').innerHTML=document.getElementById(ID.value);
	popup = document.getElementById(ID.value);
	getX = parseInt(popup.style.left)
	getY = parseInt(popup.style.top)
	getX = mouseX - getX;
	getY = mouseY - getY;
	getY = event.clientY - getY;
	getX = event.clientX - getX;
	popup.style.top=getY+'px';
	popup.style.left=getX+'px';
	mouseX = event.clientX;
	mouseY = event.clientY;
	//ID.value = "";
}
function bodyUP(event){
	var ID = document.getElementById('idPopup');
	if (!ID) return false;
	if (ID.value=="") return false;
	if (!document.getElementById(ID.value)) return false;
	ID.value = "";
}
function mouseDown(Object, event){
	var ID = document.getElementById('idPopup');
	if (!document.getElementById('idPopup')){
		return;
	}
	ID.value = Object.parentNode.id;
	//var valr  = getElementsBy("id",'popupContent.[0-9]*', Object.parentNode);
	//var valr =  getElementsByClass('imgdiv',Object);
	//alert (valr);
	//alert (Object.parentNode.id);
	//document.getElementById('popupContent').innerHTML=ID.value;
	mouseX = event.clientX;
	mouseY = event.clientY;
}
function closePopup(Object){
	//var div = Object.parentNode.parentNode
	var div = Object;
	while (true){
		if (div.tagName=="body") return;
		if (div.className=="divPopupDialog"){break;}
		div = div.parentNode;
	}
	var divParent = div.parentNode;
	divParent.removeChild(div);
	//alert (divParent.id);
	//alert (divParent.childNodes.length);
	for (i=0;i<divParent.childNodes.length;i++){
		if (divParent.childNodes[i].tagName){return;}
	}
	divParent.parentNode.removeChild(divParent);
}
function winPopup(titulo, ajax){
	var iID = idsPopup++;
	this.id="divPopup."+iID;
	this.title = titulo;
	this.width='350px';
	this.height='150px';
	this.posX='';
	this.posY='';
	this.funcion = null;
	this.ajax = ajax;
	this.draw = draw;
	this.img = "IMAGES/cancel.gif";
	this.setFuncion = function (vfun){
		this.funcion = vfun;
	}
	this.getContent = getContent;
	this.getIDContent = function (){return "popupContent."+iID;}
	function getContent(){
		var popupBG = $('divPopupBG');
		//getElementsBy(type, find,popupBG,'div') 
		var divgb =  getElementsBy("id", "popupContent."+iID,popupBG,'div') ;
		//alert (divgb[0].id);
		return divgb;
	}
	function draw(){
		if (this.posX=="") {
			var array = bodyWH();
			var posX = ((array[1]/2)-(parseInt(this.width)/2))+'px';
			var posY = ((array[0]/2)-(parseInt(this.height)/2))+'px';
			this.posX = posX;
			this.posY = posY;
		}
		sdiv="";
		//sdiv +="<div id='"+this.id+"' class='divPopupDialog' style='top:"+this.posY+";left:"+this.posX+";'>";
		sdiv +="<div class='divPopupTitle' onmousedown='mouseDown(this, event);' >";
		sdiv +="<span >"+this.title+"</span>";
		sdiv +="<img id='imgdiv'class='imgdiv' src='"+this.img+"' style='position:absolute;left:"+(parseInt(this.width)-20)+'px'+";' onclick='closePopup(this)'></img>";
		sdiv +="</div>"
		sdiv +="<div id='popupContent."+iID+"' style='background-color:white'>";
		if (this.ajax==null) sdiv += this.funcion();
		sdiv +="</div>";
		sdiv +="</div>";
		var popupWin = document.createElement('div');
		popupWin.className="divPopupDialog";
		popupWin.style.top=this.posY;
		popupWin.style.left=this.posX;
		popupWin.style.width=this.width;
		popupWin.style.height=this.height;
		popupWin.id=this.id;
		if (!document.getElementById('divPopupBG')){
			var popupBG = document.createElement('div');
			popupBG.id="divPopupBG"
			popupBG.className="divPopupBG"
			document.getElementById('divContent').parentNode.appendChild(popupBG);
			//alert (document.childNodes)
			//alert (document.getElementById('divContent').parentNode.id)
			//document.getElementById('divContent').parentNode.insertBefore(popupBG,document.getElementById('divContent'))
			//document.appendChild(popupBG);
			
		}
		else{
			var popupBG =document.getElementById('divPopupBG');
			//alert ("Cogee")
		}
		popupWin.innerHTML=sdiv;
		popupBG.appendChild(popupWin);
		
		if (this.ajax!=null){
			var divContent =  getElementsBy("id", "popupContent."+iID,popupBG,'div')[0];
			loading(divContent);
			//alert (sdiv);
			var ajax = new Ajax(this.ajax);
			ajax.Sincrono=true
			ajax.Funcion = function(){
				//function loadMenu(rq, ids){
				//alert (rq.readyState);
				//var ids = loading();
				//alert (ids);
				//alert (ajax.getRequest());
				var rq = ajax.getRequest();
				if (rq.readyState==4){
					if(rq.status==200) {
						var response = rq.responseText;
						divContent.innerHTML = response;
						//alert (response);
						var ids = loading(divContent);
						unloading(ids);
					}
				}
				//if (rq.)
			}
		}
		//var ids = loading(document.getElementById('cargar'));
		//var ids = loading();
		//ajax.anexo = ids;
		ajax.execute();	
	}
}


function loading(node, str){
	var ids = "divLoading."+(idsLoading++);
	if (!node) node=document.getElementsByTagName('body')[0];
	if (!str) str="Cargando...";
	var divLoad = document.createElement('div');
	divLoad.id=ids;
	divLoad.className="loading";
	divLoad.style.position="absolute";
	divLoad.style.zindex=2;
	//divLoad.style.display='inline';
	//divLoad.style.width='90px';
	divLoad.style.top='1px';
	divLoad.style.left='1px';
	divLoad.style.opacity=1.0;
	divLoad.innerHTML="<div style='background-color:#bb0000;padding:1px;'><img src='"+_PATH+"images/progress3.gif'>"+str+"</img></div>";
	//alert (node.childNodes[0]);
	if (node.childNodes.length>1){
		node.insertBefore(divLoad, node.childNodes[0])
	}
	else{
		node.appendChild(divLoad);
	}
	return ids;
}
function unloading(id, time){
	if (!time) var time=1.00;
	var div = document.getElementById(id);
	time = Math.round(time*100)/100;
	div.style.opacity=time;
	//var pru = document.getElementById('prueba');
	//pru.innerHTML=time;
	if (time>0.1)
		setTimeout("unloading('"+id+"',"+(time-0.03)+")",20);
	else{
		document.getElementById(id).parentNode.removeChild(div);
	}
}
function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
		if (arguments.length == 1)
			return element;
		elements.push(element);
	}
	return elements;
}
function $$(element) {
	return  document.getElementById(element);
}
function txtClick(txt, text){
	if (!text) text = "usuario";
	if (txt.value==text) {txt.value='';}
	txt.style.opacity="1.0";
}
function txtClicklost(txt){
	txt.style.opacity="0.5";
}

function getPageHTML(page, div){
	//div.innerHTML="...";
	loading(div);
	var ajax = new Ajax(page);
	ajax.Sincrono=true
	ajax.Funcion = function(){
		var rq = ajax.getRequest();
		if (rq.readyState==4){
			if(rq.status==200) {
				var response = rq.responseText;
				div.innerHTML = response;
				//alert ("Response->"+response);
				var ids = loading(div);
			}
			else if (rq.status==0){
				
			}
			else{
				div.innerHTML = "<h2>Error al cargar archivo del servidor... #"+rq.status+"</h2>";
				//div.innerHTML = "<h2>Error al cargar Archivo del servidor..."+rq.readyState+":"+rq.status+"</h2>";
			}
			unloading(ids);
		}
	}
	ajax.execute();	
	return ajax;
}
function openPopup(file, title, width){
	if (title==null) title=_TITLE;
	if (width==null) width="350";
	var ventana	=new winPopup(title);
	ventana.ajax = file;
	ventana.width=width+"px";
	ventana.draw();
}
function round(numero, d){
	if (d==null) d = 2;
	var d = Math.pow(10,d);
	var original=parseFloat(numero);
	var result=Math.round(original*100)/100 ;
	return result;
}
function printDiv(div){
  //var div = document.getElementById(nombre);
  var ventimp = window.open(' ', 'Imprimir');
  ventimp.document.write( div.innerHTML );
  ventimp.document.close();
  ventimp.print( );
  ventimp.close();
} 